package com.socket.batch.transmit;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by kaddddd on 2017/7/10.
 */
public class TransmitterFactory {
    /**
     * 已经实例化过的处理器
     */
    private static ITransmitter transmitter = null;

    private static Lock transmitterMapLock = new ReentrantLock();

    /**
     * 取得收送处理器的实例
     * @return
     */
    public static ITransmitter getTransmitter(String handlerName){

        //看是否已经有实例化对象了
        if(transmitter != null){
            return transmitter;
        }
        transmitterMapLock.lock();
        try{
            //根据name从配置信息里看是否有读取到对应的配置类
            String handlerClass = BatchTransmitter.class.getName();
            if(("socket").equals(handlerName)){
                handlerClass = BatchTransmitter.class.getName();
            }else if (("persist").equals(handlerName)){
                handlerClass = BatchTransmitter.class.getName();
            }else if (("default").equals(handlerName)) {
                handlerClass = DefaultTransmitter.class.getName();
            }
            Class<?> clazz = null;
            try{

                clazz = Thread.currentThread().getContextClassLoader().loadClass(handlerClass);

                transmitter = (ITransmitter)clazz.newInstance();

            }catch (Throwable cfe){

            }

        }finally {
            transmitterMapLock.unlock();
        }

        return transmitter;
    }
}
